/***************************************************************************
 *   Copyright (C) 2008 by Hector Francisco Hernandez                      *
 *   hectorfh@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "car_row.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <curses.h>
#include <signal.h>
#include <unistd.h>

#define STAGE_LINES 16
#define STAGE_COLS 35

/*
struct car {
  int line;
  int offset;
  int cant;
  int sep;
  int veldiv;
//  void (*drawcar)(struct car *a);
};
*/
static void finish(int sig);
void drawcar(struct car *car);

static const char *stage[] = {
"    .-.   .-.   .-.   .-.   .-.    ",
"----' '---' '---' '---' '---' '----",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"===================================",
"                                   ",
"                                   ",
"_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _",
"                                   ",
"                                   ",
"                                   ",
"==================================="};

const chtype car0[] = {'(', '#'};

/*
struct car car[] = {*/
  /*{{'(', '#', '#', '#', ')'}},
  {{*'(', '#', '#', '#', ')'}},*/
/*
  {11, 13, 3,  6, 30}
};
*/

int frogx, frogy;

/*
static const chtype car[][] = {{'(' | COLOR_PAIR(1), '#' | COLOR_PAIR(1),
                              '#' | COLOR_PAIR(1), '#' | COLOR_PAIR(1),
                              ')' | COLOR_PAIR(1)},
                              {'(' | COLOR_PAIR(1), '#' | COLOR_PAIR(1),
                              '#' | COLOR_PAIR(1), '#' | COLOR_PAIR(1),
                              ')' | COLOR_PAIR(1)},
                              {'(' | COLOR_PAIR(1), '#' | COLOR_PAIR(1),
                              '#' | COLOR_PAIR(1), '#' | COLOR_PAIR(1),
                              ')' | COLOR_PAIR(1)}};
*/

WINDOW *stagewin, *xwin;
int tick;


main(int argc, char *argv[])
{
    int i;

    frogx = 0;
    frogy = 0;
    tick = 0;

    srand(time(NULL));

    /* initialize your non-curses data structures here */

    (void) signal(SIGINT, finish);      /* arrange interrupts to terminate */

    (void) initscr();      /* initialize the curses library */
    keypad(stdscr, TRUE);  /* enable keyboard mapping */
    (void) nonl();         /* tell curses not to do NL->CR/NL on output */
    (void) cbreak();       /* take input chars one at a time, no wait for \n */
    (void) noecho();       /* don't echo input */
    curs_set(0);

    start_color();

    init_pair(1, COLOR_GREEN, COLOR_BLACK);
    init_pair(2, COLOR_BLUE, COLOR_BLACK);

    xwin = newwin(16, 35, 0, 10);
    stagewin = newwin(16, 35, 0, 0);
    nodelay(stagewin, TRUE);

    for (i = 0; i < STAGE_LINES; i++) {
      mvwaddstr(stagewin, i, 0, stage[i]);
    }
/*
    for (i = 0; i < STAGE_LINES; i++) {
      mvwaddstr(xwin, i, 0, "a");
    }
*/
    struct car_row *car_row = car_row_create(stagewin, -35, 10, 105, 0);
    car_row_draw(car_row);

//    drawcar(&car[0]);

    frogx = 17;
    frogy = 13;
    mvwaddch(stagewin, frogy, frogx, COLOR_PAIR(1) | '@');

    //drawcar(stdscr, 0, car[0], sizeof(car1) / sizeof(chtype), 30 ,-10);
    //drawcar(stdscr, 1, car[1], sizeof(car1) / sizeof(chtype), 30 ,0);
    //drawcar(stdscr, 2, car[2], sizeof(car1) / sizeof(chtype), 30 ,-20);

    //touchwin(stagewin);
    wrefresh(xwin);
    wrefresh(stagewin);

  for (;;)
  {
    int c = wgetch(stagewin);
    usleep(10000);
    //touchwin(stagewin);
    //wrefresh(stagewin);

    //drawcar(&car[0]);
mvwaddstr(stagewin, 10, 0, "                                   ");
    car_row_move(car_row);
    car_row_draw(car_row);
    mvwaddch(stagewin, frogy, frogx, stage[frogy][frogx]);
    frogx += c == 'l' && frogx < 34 ? 1 : c == 'h' && frogx > 0 ? -1 : 0;
    frogy += c == 'j' && frogy < 13 ? 1 : c == 'k' && frogy > 0 ? -1 : 0;
    mvwaddch(stagewin, frogy, frogx, COLOR_PAIR(1) | '@');

    tick ++;
  }

    finish(0);               /* we're done */
}



static void finish(int sig)
{
    endwin();

    /* do your non-curses wrapup here */

    exit(0);
}


/*
void drawcar(struct car *car)
{
  int y, x, i;

  if (tick % car->veldiv != 0) {
    return;
  }

  y = car->line;

  mvwaddstr(stagewin, y, 0, "                                   ");

  x = car->offset = (car->offset + 1) % 35;

  for (i = 0; i < car->cant; i++) {
    mvwaddch(stagewin, y, x = (x + 1) % 35, '(');
    mvwaddch(stagewin, y, x = (x + 1) % 35, '#');
    mvwaddch(stagewin, y, x = (x + 1) % 35, '#');
    mvwaddch(stagewin, y, x = (x + 1) % 35, '#');
    mvwaddch(stagewin, y, x = (x + 1) % 35, ')');

    x = (x + car->sep) % 35;
  }
}
*/
